package com.freecloud.javabasics.thread.CountDownLatch;

import java.util.concurrent.CountDownLatch;

/**
 * 使用场景：计数器
 *
 * @Author: maomao
 * @Date: 2020-02-23 17:16
 */
public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(3);

        new Thread(()-> {
            System.out.println("Thread1");
            countDownLatch.countDown();  //count - 1 = 2
        }).start();

        new Thread(()-> {
            System.out.println("Thread2");//count - 1 = 1
            countDownLatch.countDown();
        }).start();

        new Thread(()-> {
            System.out.println("Thread3");//count - 1 = 0
            countDownLatch.countDown();
        }).start();

        //阻塞多个线程，直到count为0时，继续执行之后
        countDownLatch.await();

        System.out.println("CountDownLatch End!");
    }
}
